package com.itheima.service.mongo.impl;

import com.itheima.domain.mongo.Friend;
import com.itheima.domain.mongo.RecommendUser;
import com.itheima.domain.mongo.UserLike;
import com.itheima.domain.mongo.Visitor;
import com.itheima.service.mongo.UserLikeService;
import com.itheima.vo.PageBeanVo;
import com.itheima.vo.UserLikeCountVo;
import jdk.nashorn.internal.runtime.linker.LinkerCallSite;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.ArrayList;
import java.util.List;

@Service
public class UserLikeServiceImpl implements UserLikeService {

    @Autowired
    private MongoTemplate mongoTemplate;


    //添加喜欢记录
    @Override
    public void save(Long userId, Long likeUserId) {
        // 1.判断二人是否存在这个喜欢记录
        Query query = new Query(Criteria.where("userId").is(userId).and("likeUserId").is(likeUserId));
        boolean exists = mongoTemplate.exists(query, UserLike.class);
        //2.如果不存在。再去保存
        if(!exists){
            UserLike userLike = new UserLike();
            userLike.setUserId(userId);
            userLike.setLikeUserId(likeUserId);
            userLike.setCreated(System.currentTimeMillis());
            mongoTemplate.save(userLike);
        }
    }

    //判断二人是否相互喜欢
    @Override
    public boolean isMutualLike(Long userId, Long likeUserId) {
        //1.判断你是否喜欢她
        Query query1 = new Query(Criteria.where("userId").is(userId).and("likeUserId").is(likeUserId));
        boolean exists1 = mongoTemplate.exists(query1, UserLike.class);
        //2.判断她是否喜欢你
        Query query2 = new Query(Criteria.where("userId").is(likeUserId).and("likeUserId").is(userId));
        boolean exists2 = mongoTemplate.exists(query2, UserLike.class);

        return exists1&exists2;
    }

    //删除mongo中的喜欢记录
    @Override
    public void delete(Long userId, Long likeUserId) {
        //查询是否存在喜欢记录
        Query query = new Query(Criteria.where("userId").is(userId).and("likeUserId").is(likeUserId));
        boolean exists = mongoTemplate.exists(query, UserLike.class);
        if(exists){
            mongoTemplate.remove(query,UserLike.class);
        }
    }


    //统计 好友  喜欢 粉丝数量
    @Override
    public UserLikeCountVo findUserLikeCount(Long userId) {
        //统计相互喜欢数量：查询好友列表 (从friend表中根据userId查询我的所有好友数量)
        Query query1 = new Query(Criteria.where("userId").is(userId));
        long count1 = mongoTemplate.count(query1, Friend.class);

        //统计喜欢数量：查询你喜欢的用户 (从user_like表中根据userId查询所有我喜欢的用户数量）
        Query query2 = new Query(Criteria.where("userId").is(userId));
        long count2 = mongoTemplate.count(query2, UserLike.class);

        //统计粉丝数量：查询别人喜欢你的用户 （从user_like表中根据userId查询所有喜欢我的用户数
        Query query3 = new Query(Criteria.where("likeUserId").is(userId));
        long count3 = mongoTemplate.count(query3, UserLike.class);

        //组装UserLikeCountVo
        UserLikeCountVo userLikeCountVo = new UserLikeCountVo();
        userLikeCountVo.setEachLoveCount(count1);
        userLikeCountVo.setLoveCount(count2);
        userLikeCountVo.setFanCount(count3);

        //返回结果
        return userLikeCountVo;
    }

    //相互喜欢：查询好友列表
    @Override
    public PageBeanVo findEachLove(Integer pageNum, Integer pageSize, Long userId) {
        // 1、构造查询条件
        Integer index = (pageNum - 1) * pageSize;
        Query query = Query.query(Criteria.where("userId").is(userId)).skip(index).limit(pageSize);

        //查询好友
        List<Friend> friendList = mongoTemplate.find(query,Friend.class);

        //3、转换 recommend
        List<RecommendUser> userList= new ArrayList<>();
        for (Friend friend : friendList) {
            RecommendUser user = changeRecommendUser(friend.getUserId(), friend.getFriendId());
            userList.add(user);
        }
        long counts = mongoTemplate.count(query, Friend.class);
        // 4、返回分页对象
        return new PageBeanVo(pageNum, pageSize, counts, userList);
    }

    //查询你喜欢用户列表
    @Override
    public PageBeanVo findLove(Integer pageNum, Integer pageSize, Long userId) {
        // 1、构造条件
        Integer index = (pageNum - 1) * pageSize;
        Query query = Query.query(Criteria.where("userId").is(userId)).skip(index).limit(pageSize);
        // 2、查询你喜欢的
        List<UserLike> userLikeList = mongoTemplate.find(query, UserLike.class);
        // 3、转换 recommend
        List<RecommendUser> userList = new ArrayList<>();
        for (UserLike userLike : userLikeList) {
            RecommendUser user = changeRecommendUser(userLike.getUserId(), userLike.getLikeUserId());
            userList.add(user);
        }
        long counts = mongoTemplate.count(query, UserLike.class);
        // 4、返回分页
        return new PageBeanVo(pageNum, pageSize, counts, userList);
    }

    //查询粉丝用户列表
    @Override
    public PageBeanVo findFan(Integer pageNum, Integer pageSize, Long userId) {
        // 1、构造条件
        Integer index = (pageNum - 1) * pageSize;
        Query query = Query.query(Criteria.where("likeUserId").is(userId)).skip(index).limit(pageSize);
        // 2、查询你喜欢的
        List<UserLike> userLikeList = mongoTemplate.find(query, UserLike.class);
        // 3、转换 recommend
        List<RecommendUser> userList = new ArrayList<>();
        for (UserLike userLike : userLikeList) {
            RecommendUser user = changeRecommendUser(userLike.getLikeUserId(), userLike.getUserId());
            userList.add(user);
        }
        long counts = mongoTemplate.count(query, UserLike.class);
        // 4、返回分页
        return new PageBeanVo(pageNum, pageSize, counts, userList);
    }

    //查询历史访客用户列表
    @Override
    public PageBeanVo findVisitor(Integer pageNum, Integer pageSize, Long userId) {
        // 1、构造条件
        Integer index = (pageNum - 1) * pageSize;
        Query query = Query.query(Criteria.where("userId").is(userId)).skip(index).limit(pageSize);
        // 2、查询历史访客
        List<Visitor> visitorList = mongoTemplate.find(query, Visitor.class);
        // 3、转换 recommend
        List<RecommendUser> userList = new ArrayList<>();
        for (Visitor visitor : visitorList) {
            RecommendUser user = changeRecommendUser(visitor.getUserId(), visitor.getVisitorUserId());
            userList.add(user);
        }
        long counts = mongoTemplate.count(query, Visitor.class);
        // 4、返回分页
        return new PageBeanVo(pageNum, pageSize, counts, userList);
    }


    // 抽取一个方法，转为recommendUser
    private RecommendUser changeRecommendUser(Long toUserId, Long userId) {
        Query query = Query.query(Criteria.where("toUserId").is(toUserId).and("userId").is(userId));
        RecommendUser recommendUser = mongoTemplate.findOne(query, RecommendUser.class);
        if (recommendUser == null) { // 构建默认数据
            recommendUser = new RecommendUser();
            recommendUser.setToUserId(toUserId);
            recommendUser.setUserId(userId);
            recommendUser.setScore(95D);
        }
        return recommendUser;
    }
}
